package com.dounine.search;

/**
 * Created by huanghuanlai on 16/4/13.
 */
public class ConsoleProgressBar {
    private int index = 0;
    private static String PRE = "";
    public static boolean CONTAINER = true;
    private String showChars[] = {".", "..", "...", "....", ".....", "......", "......."}; // 用于进度条显示的字符

    public static void changeTxt(String txt){
        synchronized (PRE){
            PRE = txt;
        }
    }

    public void show() {
        reset();
        if (index >= showChars.length) {
            index = 0;
        }
        draw(showChars[index++]);
    }

    public void begin() {
        try {
            while (CONTAINER) {
                show();
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void draw(String rate) {
        synchronized (this){
            System.out.print(' ');
            System.out.print(PRE + rate);
        }
    }

    private void reset() {
        System.out.print('\r');
    }

    public static void main(String[] args) throws InterruptedException {
        ConsoleProgressBar cpb = new ConsoleProgressBar();
        ConsoleProgressBar.PRE = "搜索用户中";
        cpb.begin();
    }

}
